package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 会话资源控制器
 */
@RestController
@RequestMapping("/sessions")
public class SessionController {
    @Autowired
    private IUserService userService;
    
    @PostMapping
    public JsonResult login(User user){
        JsonResult json = new JsonResult();
        try {
            User currentUser = userService.login(user);
            json.setData(currentUser);
            System.out.println(UserContext.getCurrentUser());
        } catch (Exception e) {
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }

    @DeleteMapping
    public void logout(){
        UserContext.getSession().invalidate();
    }


    @GetMapping
    public JsonResult get(){
        JsonResult j=new JsonResult();
        System.out.println(UserContext.getCurrentUser());
        j.setUser(UserContext.getCurrentUser());
        return j;
    }
}
